﻿class lrpcCore {
    _id = 0;
    _lrpcChannels = [];

    constructor() {
        this._id = new Date().getTime();
    }

    get id() {
        return this._id;
    }


    __validateId(id) {
        if (!jsHelper.isInteger(id)) {
            throw "远程调用通道id必须为整数";
        }
    }

    __findIndexById(id) {
        for (let i = 0; i < this._lrpcChannels.length; i++) {
            if (this._lrpcChannels[i].id == id) {
                return i;
            }
        }

        return -1;
    }

    createChannel(id, pro) {
        this.__validateId(id);

        if (pro == null || pro == undefined) {
            throw "pro不能为未定义";
        }

        if (this.__findIndexById(id) >= 0) {
            throw "已存在id为" + id + "的通道";
        }

        this._lrpcChannels.push(new lrpcChannel(id, pro));
    }


    tryCreateChannel(id, pro) {
        this.__validateId(id);

        if (pro == null || pro == undefined) {
            throw "pro不能为未定义";
        }

        if (this.__findIndexById(id) >= 0) {
            return;
        }

        this._lrpcChannels.push(new lrpcChannel(id, pro));
    }


    existChannel(id) {
        this.__validateId(id);

        if (this.__findIndexById(id) >= 0) {
            return true;
        }

        return false;
    }

    getChannelIds() {
        let channelIds = [];
        for (let i = 0; i < this._lrpcChannels.length; i++) {
            channelIds.push(this._lrpcChannels[i].id);
        }

        return channelIds;
    }

    deleteChannel(id) {
        this.__validateId(id);

        let index = this.__findIndexById(id);
        if (index < 0) {
            return;
        }

        this._lrpcChannels.splice(index, 1);
    }


    remoteCallF(id, obj) {
        this.__validateId(id);

        let index = this.__findIndexById(id);
        if (index < 0) {
            throw "id为" + id + "的远程调用通道未创建";
        }

        let channel = this._lrpcChannels[index];
        return channel.onRaiseProF(obj);
    }

    remoteCallA(id, obj) {
        this.__validateId(id);

        let index = this.__findIndexById(id);
        if (index < 0) {
            throw "id为" + id + "的远程调用通道未创建";
        }

        let channel = this._lrpcChannels[index];
        channel.onRaiseProA(obj);
    }

    tryRemoteCallF(id, obj) {
        this.__validateId(id);

        let index = this.__findIndexById(id);
        if (index < 0) {
            return;
        }

        let channel = this._lrpcChannels[index];
        channel.onRaiseProF(obj);
    }

    tryRemoteCallA(id, obj) {
        this.__validateId(id);

        let index = this.__findIndexById(id);
        if (index < 0) {
            return;
        }

        let channel = this._lrpcChannels[index];
        channel.onRaiseProA(obj);
    }




    static _instance = null;
    static get instance() {
        return lrpcCore._instance;
    }
    static set instance(value) {
        if (!jsHelper.hasValue(value)) {
            throw "instance不能为空";
        }

        lrpcCore._instance = value;
    }
}

class lrpcChannel {
    _id;
    get id() {
        return this._id;
    }

    _pro = null;

    constructor(id, pro) {
        this._id = id;
        this._pro = pro;
    }

    onRaiseProA(obj) {
        this._pro(obj);
    }

    onRaiseProF(obj) {
        return this._pro(obj);
    }
}